package com.app.newiptv.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.adaper.RecodeCollectAdapter;
import com.app.newiptv.bean.RecodeCollectBean;
import com.app.newiptv.helper.MySQLiteOpenHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class RecodeAndCollectActivity extends Activity {
    private SQLiteDatabase readInstance, writeInstance;
    private TextView title;
    private Button clearAll;
    private RecyclerView recodeCollectRecycle;
    public Integer type;
    private List<RecodeCollectBean> recodeCollectBeanList = new ArrayList<>();
    private RecodeCollectAdapter adapter;
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final public static String TAG = "RecodeAndCollect";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_recode_collect);
        type = getIntent().getIntExtra("type", 0);
        initView();
        MySQLiteOpenHelper sqlHelper = MySQLiteOpenHelper.getInstance(this);
        readInstance = sqlHelper.getReadInstance();
        writeInstance = sqlHelper.getWriteInstance();
        Log.i(TAG, "onCreate: type=" + type);

    }

//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//        if (newConfig.fontScale != 1)//非默认值
//            getResources();
//        super.onConfigurationChanged(newConfig);
//    }
//
//    @Override
//    public Resources getResources() {
//        Resources res = super.getResources();
//        if (res.getConfiguration().fontScale != 1) {//非默认值
//            Configuration newConfig = new Configuration();
//            newConfig.setToDefaults();//设置默认
//            res.updateConfiguration(newConfig, res.getDisplayMetrics());
//        }
//        return res;
//    }

    private void initView() {
        title = (TextView) findViewById(R.id.activity_recode_collect_title);
        clearAll = (Button) findViewById(R.id.activity_recode_collect_clear_all);
        title.setText(type==0?"观看记录":"我的收藏");
        recodeCollectRecycle = (RecyclerView) findViewById(R.id.activity_recode_collect_recycle);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recodeCollectRecycle.setLayoutManager(layout);
        adapter = new RecodeCollectAdapter(recodeCollectBeanList, this, type);
        recodeCollectRecycle.setAdapter(adapter);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 清空观看记录或者收藏
                AlertDialog.Builder builder = new AlertDialog.Builder(RecodeAndCollectActivity.this);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        writeInstance.delete("RecodeCollect", "type="+type, null);
                        recodeCollectBeanList.clear();
                        adapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setTitle("提示");
                builder.setMessage("确定要删除这条记录吗？");
                builder.show();

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        recodeCollectBeanList.clear();
        Cursor cursor = readInstance.query("RecodeCollect", null, "type=" +type, null, null, null, "collect_time desc");
        if (cursor.moveToFirst()) {
            do{
                String pic_url = cursor.getString(cursor.getColumnIndex("pic_url"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String actor = cursor.getString(cursor.getColumnIndex("actor"));
                Long collect_time = cursor.getLong(cursor.getColumnIndex("collect_time"));
                Integer type = cursor.getInt(cursor.getColumnIndex("type"));
                Integer play_time = cursor.getInt(cursor.getColumnIndex("play_time"));
                Integer movie_id = cursor.getInt(cursor.getColumnIndex("movie_id"));
                Integer movie_type = cursor.getInt(cursor.getColumnIndex("movie_type"));
                Integer sitcom_index = cursor.getInt(cursor.getColumnIndex("sitcom_index"));
                recodeCollectBeanList.add(new RecodeCollectBean(pic_url, name, actor, dateformat.format(collect_time), type, play_time, movie_id, movie_type, sitcom_index));
            }while(cursor.moveToNext());
        }
        adapter.notifyDataSetChanged();
    }
}
